/**
 * 15/02/2014
 * 
 * StateController
 */
package org.cuatrovientos.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.StockBOInterface;
import org.cuatrovientos.erp.model.Stock;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Controller class form Stock management
 * @author Inaki Torres
 */
@Controller
public class StockController {

	//Get log4j logger
	private static final Logger logger = 
			Logger.getLogger(StockController.class);

	@Autowired
	private StockBOInterface stockBO;

	/**
	 * default constructor
	 */
	public StockController () {
		logger.info("Stock BEAN instantiated");
	}

	/**
	 * handles default /stock or /stock/list request
	 * @return
	 */
	@RequestMapping({"/stock","/stock/list"})
	public ModelAndView stockDefault() {
		logger.info("default");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Stock> stockList = stockBO.selectAll();
		model.put("stocks", stockList );
		
		return new ModelAndView("StockList", model);     
	}


	/**
	 * handles default /stock/search request
	 * @param name
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/stock/search"})
	public ModelAndView search(
			@RequestParam(value="name", defaultValue="", required=true) 
			String description) {
		
		logger.info("search");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Stock> stockList = stockBO.search(description);
		model.put("stocks", stockList );
		
		return new ModelAndView("StockList", model);     
	}

	/**
	 * handles default /stock/delete request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/stock/delete"})
	public String deleteStock(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("delete " + id);

		stockBO.deleteById(id);
		
		return "redirect:/stock";
	}

	/**
	 * handles default /stock/new request
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/stock/new"})
	public ModelAndView newStock() {
		logger.info("new stock");
		
		Stock stock = new Stock();

		return new ModelAndView("NewStock", "stock", stock);
	}

	/**
	 * handles default /stock/save request
	 * @param stock
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/stock/save"})
	public String newStock(@Valid Stock stock, BindingResult bindingResult) {
		logger.info("new stock " + stock.toString());

		//In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("stock", stock);
			
			return "NewStock";
		}

		stockBO.insert(stock);
		
		return "redirect:/stock";
	}

	/**
	 * handles default /stock/detail request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/stock/detail"})
	public ModelAndView stockDetail(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("Detail " + id);
		
		Map<String,Object> model = new HashMap<String,Object>();
		Stock stock = stockBO.selectById(id);
		model.put("stock", stock);

		return new ModelAndView("StockDetail", model);
	}


	/**
	 * handles default /stock/update request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/stock/update"})
	public ModelAndView update(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("update " + id);
		
		Map<String,Object> model = new HashMap<String,Object>();
		Stock stock = stockBO.selectById(id);
		model.put("stock", stock);
		
		return new ModelAndView("UpdateStock",model);
	}


	/**
	 * handles default /stock/saveupdate request
	 * @param stock
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/stock/saveupdate"})
	public String saveUpdate(@Valid Stock stock, BindingResult bindingResult) {
		logger.info("update stock " + stock.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("stock", stock);
			
			return "UpdateStock";
		}

		stockBO.update(stock);
		
		return "redirect:/stock";
	}
	
	/**
	 * @param model
	 * @return
	 */
	@RequestMapping({"/stock/index"})
	public String sayHelloPage(Map<String, Object> model) {
		return "index/IndexIssuetype";
	}
	
}
